package com.agan.system.service.impl;

import java.util.Arrays;
import java.util.Date;

import com.agan.common.core.utils.StringUtils;
import com.agan.system.param.ListNoticeParam;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.agan.system.domain.SysNotice;
import com.agan.system.mapper.SysNoticeMapper;
import com.agan.system.service.ISysNoticeService;

/**
 * 公告 服务层实现
 * 
 * @author agan
 */
@Service
@RequiredArgsConstructor
public class SysNoticeServiceImpl extends ServiceImpl<SysNoticeMapper, SysNotice> implements ISysNoticeService
{
    private final SysNoticeMapper noticeMapper;

    /**
     * 查询公告信息
     * 
     * @param noticeId 公告ID
     * @return 公告信息
     */
    @Override
    public SysNotice selectNoticeById(String noticeId)
    {
        return noticeMapper.selectById(noticeId);
    }

    /**
     * 查询公告列表
     * 
     * @param param 公告信息
     * @return 公告集合
     */
    @Override
    public IPage<SysNotice> selectNoticeList(ListNoticeParam param)
    {
        IPage<SysNotice> page = new Page<>(param.getPageNum(), param.getPageSize());

        LambdaQueryWrapper<SysNotice> sysNoticeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysNoticeLambdaQueryWrapper
                .like(StringUtils.isNotEmpty(param.getNoticeTitle()), SysNotice::getNoticeTitle, param.getNoticeTitle())
                .like(StringUtils.isNotEmpty(param.getNoticeType()), SysNotice::getNoticeType, param.getNoticeType())
                .like(StringUtils.isNotEmpty(param.getCreateBy()), SysNotice::getCreateBy, param.getCreateBy())
                .orderByDesc(SysNotice::getCreateTime);

        return noticeMapper.selectPage(page, sysNoticeLambdaQueryWrapper);
    }

    /**
     * 新增公告
     * 
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public int insertNotice(SysNotice notice)
    {
        notice.setCreateTime(new Date());
        return noticeMapper.insert(notice);
    }

    /**
     * 修改公告
     * 
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public int updateNotice(SysNotice notice)
    {
        LambdaUpdateWrapper<SysNotice> sysNoticeLambdaUpdateWrapper = new LambdaUpdateWrapper<>();

        sysNoticeLambdaUpdateWrapper
                .set(StringUtils.isNotEmpty(notice.getNoticeTitle()), SysNotice::getNoticeTitle, notice.getNoticeTitle())
                .set(StringUtils.isNotEmpty(notice.getNoticeType()), SysNotice::getNoticeType, notice.getNoticeType())
                .set(StringUtils.isNotEmpty(notice.getNoticeContent()), SysNotice::getNoticeContent, notice.getNoticeContent())
                .set(StringUtils.isNotEmpty(notice.getStatus()), SysNotice::getStatus, notice.getStatus())
                .set(StringUtils.isNotEmpty(notice.getUpdateBy()), SysNotice::getUpdateBy, notice.getUpdateBy())
                .set(SysNotice::getUpdateTime, new Date())
                .eq(SysNotice::getNoticeId, notice.getNoticeId());

        return noticeMapper.update(notice, sysNoticeLambdaUpdateWrapper);
    }

    /**
     * 删除公告对象
     * 
     * @param noticeId 公告ID
     * @return 结果
     */
    @Override
    public int deleteNoticeById(String noticeId)
    {
        return noticeMapper.deleteById(noticeId);
    }

    /**
     * 批量删除公告信息
     * 
     * @param noticeIds 需要删除的公告ID
     * @return 结果
     */
    @Override
    public int deleteNoticeByIds(String[] noticeIds)
    {
        return noticeMapper.deleteBatchIds(Arrays.asList(noticeIds));
    }
}
